/*
 *  @ProductName@
 *  Copyright (C) @CopyrightYear@
 *    National Institute of Advanced Industrial Science and Technology (AIST)
 *    Registration Number: @AISTRegistrationNumber@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.vuln.core.scap.nvd.rest;

import jp.go.aist.six.vuln.model.scap.vulnerability.Vulnerability;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;




/**
 *
 * @author  Akihito Nakamura, AIST
 * @version $Id: VulnerabilityResource.java 210 2010-06-02 03:57:40Z nakamura5akihito $
 */
public class VulnerabilityResource
    extends NvdBaseResource
{

    /**
     * Logger.
     */
    private static Log  _LOG = LogFactory.getLog( VulnerabilityResource.class );



    private String  _vulnID;

    private Vulnerability  _vuln;



    /**
     * Constructor.
     */
    public VulnerabilityResource(
                    final Context context,
                    final Request request,
                    final Response response
                    )
    {
        super( context, request, response );


        _vulnID = (String)getRequest().getAttributes().get( "vulnerabilityID" );
        try {
            _vuln = _getNvdService().getVulnerability( _vulnID );
        } catch (Exception ex) {
            //TODO:
        }

        if (_vuln == null) {
            setAvailable( false );
        } else {
            getVariants().add( new Variant( MediaType.APPLICATION_XML) );
        }
    }



    //**************************************************************
    //  Resource
    //**************************************************************

    // GET: returns the Vulnerability
    @Override
    public Representation represent(
                    final Variant variant
                    )
    throws ResourceException
    {
        String  xml = null;
        try {
            xml = _getNvdXml().marshalToString( _vuln );
        } catch (Exception ex) {
            throw new ResourceException( Status.SERVER_ERROR_INTERNAL );
        }

        if (_LOG.isTraceEnabled()) {
            _LOG.trace( "REST response: vulnerability=" + _vuln.getPersistentID() );
        }

        if (xml == null) {
            throw new ResourceException( Status.CLIENT_ERROR_NOT_FOUND );
        }

        return (new StringRepresentation( xml ));
    }

}
// VulnerabilityResource

/* vim:set tabstop=4:set expandtab:set shiftwidth=4: */

